package com.atguigu.spring5.jdbctemplate.dao;

import com.atguigu.spring5.jdbctemplate.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

@Repository
public class BookDaoImpl implements BookDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(Book book) {
        // update 实现对数据库的增删改操作
        //1 创建 sql 语句
        String sql = "insert into t_book values(?,?,?)";
        //2 调用方法实现
        Object[] args = {book.getBookId(), book.getBookName(), book.getBookStatus()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println("添加行数" + update);
    }

    @Override
    public void update(Book book) {
        String sql = "update t_book set book_id = ?,book_name = ?,book_status = ?";
        Object[] args = {book.getBookId(), book.getBookName(), book.getBookStatus()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println("修改行数" + update);

    }

    @Override
    public void delete(String id) {
        String sql = "delete from t_book where book_id=?";
        int update = jdbcTemplate.update(sql, id);
        System.out.println("删除行数" + update);
    }

    @Override
    public int selectCount() {
        String sql = "select count(1) from t_book where book_id = ?";
        Object[] args = {"1"};
        Integer count = jdbcTemplate.queryForObject(sql, args, Integer.class);
        return count;
    }

    @Override
    public Book findBookInfo(String id) {
        String sql = "select * from t_book where book_id=?";
        // 通过RowMapper的实现类 BeanPropertyRowMapper ，设置我们需要的返回对象为Book
        // BeanPropertyRowMapper内部将sql的结果数据获取到，然后调用指定类中的set方法，依次将值set到类中
        Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
        return book;
    }

    @Override
    public List<Book> findAllBookInfo() {
        String sql = "select * from t_book where 1=?";
        Object[] args = {"1"};
        List<Book> books = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class),args);
        return books;
    }

    @Override
    public int[] batchAdd(List<Object[]> batchArgs) {
        String sql = "insert into t_book values(?,?,?)";
        int[] nums = jdbcTemplate.batchUpdate(sql,batchArgs);
        System.out.println("批量添加结果："+ Arrays.toString(nums));
        return nums;
    }

    @Override
    public int[] batchUpdate(List<Object[]> batchArgs) {
        String sql = "update t_book set book_name=?,book_status=? where book_id=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println("批量修改结果："+ Arrays.toString(ints));
        return ints;
    }

    @Override
    public int[] batchDelete(List<Object[]> batchArgs) {
        String sql = "delete from t_book where book_id=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println("批量删除结果："+ Arrays.toString(ints));
        return ints;
    }


}
